<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Http\Requests\Api\V1\MemberRequest;
use App\Services\Member\MemberService;

use App\Contracts\Sms\SmsCacheContracts;
use App\Contracts\Product\CartItemsContracts;

use App\Models\Users;
use App\Models\Product;
use App\Models\MemberAddress;

use Tymon\JWTAuth\Facades\JWTAuth;
use App\Models\CartItem;

use Illuminate\Support\Facades\Validator;

class MemberController extends Controller
{

    protected $MemberService;
    protected $UsersModel;
    public function __construct() {

        $this->MemberService = new MemberService();
        $this->UsersModel = new Users();
    }

    public function cartList(CartItemsContracts $CartItemsContracts)
    {
        $user = JWTAuth::parseToken()->touser();
        $list = $CartItemsContracts->cartList($user);

		return $this->response($list);
    }

    public function test(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'user_id' => 'required|integer|user',
            'amount' => 'required|integer'
            ]);
        if($validate->fails())
        {
            return $validate->errors()->first();
        } 

        echo 123123;die;
    }

    //添加购物车
    public function addcart(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'product_sku_id' => 'required|integer',
            'amount' => 'required|integer'
            ]);
        if($validate->fails())
        {
            return $validate->errors()->first();
        }

        $user = JWTAuth::parseToken()->touser();
        if ($cart = $user->cartItems()->where('product_sku_id',$request->input('product_sku_id'))->first()){
            //如果购物车中已经加入了这个商品,那么处理变为修改之前的商品的数量,加上本次的数量
            $cart->update([
                'amount' => $cart->amount + $request->input('amount'),
            ]);
        }else{
            $cart = new CartItem(['amount' => $request->input('amount')]);
            $cart->user()->associate($user);
            $cart->productSku()->associate($request->input('product_sku_id'));
            $cart->save();
        }

        return [
            'code' => 200,
            'info' => '加入购物车成功'
        ];
    }

    // 取出购物车
    public function removecart(Request $request)
    {
        $product_sku_id = $request->input('product_sku_id');
        $amount = $request->input('amount');

        $user = JWTAuth::parseToken()->touser();
        if ($cart = $user->cartItems()->where('product_sku_id',$product_sku_id)->first()){
            if($amount>$cart->amount){
                return [
                    'code' => 402,
                    'info' => '超出数量限制'
                ];
            }
            //如果购物车中已经加入了这个商品,那么处理变为修改之前的商品的数量,加上本次的数量
            $cart->update([
                'amount' => $cart->amount - $amount,
            ]);
            return [
                'code' => 200,
                'info' => '操作成功'
            ];
        }else{
            return [
                'code' => 402,
                'info' => '抱歉你还没加入购物车'
            ];
          
        }
    }

    // 收藏商品
    public function favor(Request $request)
    {

        $user = JWTAuth::parseToken()->touser();
        
        if(!$product = Product::find($request->input('product_id'))){
            return  $this->response([
                'code'=>422,
                'error'=>'该商品不存在',
            ]);
        }
        
        if(!$user->favoriteProducts()->find($product->id)){

            // laravel提供的 attach 方法 根据关联表当作数据新增
            $user->favoriteProducts()->attach($product);

            return $this->response([
                'code'=>200,
                'error'=>'商品收藏成功'
            ]);
        }else{
            return $this->response([
                'code'=>422,
                'error'=>'商品已经收藏过了'
            ]);
        }
    }

    //取消收藏
    public function disfavor(Request $request)
    {
        $user = JWTAuth::parseToken()->touser();

        if(!$product = Product::find($request->input('product_id'))){
            return $this->response([
                'code'=>422,
                'error'=>'该商品不存在',
            ]);
        }
        
        if($user->favoriteProducts()->find($product->id)){
            //取消关联
            $user->favoriteProducts()->detach($product);
            return $this->response([
                'code'=>200,
                'error'=>'取消收藏成功',
            ]);
        }else{
            return $this->response([
                'code'=>422,
                'error'=>'商品还未收藏',
            ]);
        }

    }

    public function address_list()
    {
        $userinfo = JWTAuth::parseToken()->touser();
        $memberAddress = MemberAddress::where('user_id',$userinfo['id'])->get();

        return $this->response($memberAddress);
    }

    public function store(MemberRequest $Request,SmsCacheContracts $SmsCacheContracts)
    {
        $res = $this->MemberService->register($Request,$SmsCacheContracts);
        return response()->json($res)->setStatusCode(200);
    }

    public function logout()
    {
        $res = JWTAuth::setToken(JWTAuth::getToken())->invalidate();
        return $this->response($res);
    }
    
    public function login(Request $request)
    {   
        $member = $this->MemberService->login($request);
        return $this->response($member);
    }
}
